package com.iMusic.businessLogicLayer;

import java.io.IOException;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.iMusic.businessLogicLayer.orderStatus.OrderStatus;
import com.iMusic.integrationLayer.DBConnection;
import com.iMusic.integrationLayer.DBConnectionManager;
import com.iMusic.util.UserMessages;

import java.lang.Integer;

public class Staff {

	private int staffId = 0;
	private String name = null;
	private String username = null;
	private String passwd = null;
	private String email = null;
	private Date dateCreated = null;
	private Date dateModified = null;
	private DBConnection dbConn = null;
	
	static private DBConnectionManager dbConnMgr = null;

	/*
	 * Constructor
	 */
	public Staff() {
		if (dbConnMgr == null) {
			dbConnMgr = DBConnectionManager.getDBConnectionManager();
		}
		
	}

	
	/*
	 * Login into Account
	 */
	public boolean login() throws SQLException {
		
		boolean ret = false;
		
		if (dbConn != null) {
			dbConnMgr.freeDBConnection(dbConn);
		}
		
		dbConn = dbConnMgr.getDBConnection();
		dbConn.connect();
		
		ResultSet rs = dbConn.query("SELECT * FROM STAFF WHERE USERNAME = '" + username + "'" +
										"AND PASSWORD = password('" + passwd + "')");
		
		if (rs != null && rs.next()) {
			
			setStaffId(rs.getInt("StaffId"));
			setName(rs.getString("StaffName"));
			setEmail(rs.getString("Email"));
			setDateCreated(rs.getDate("DateCreated"));
			setDateModified(rs.getDate("DateModified"));

			ret = true;
			System.out.println("Staff " + username + " logged in.");

			rs.close();
			rs = null;

			
		}
		
		return ret;
	}

	
	/*
	 * Logout
	 */
	public boolean logout() {
		boolean ret = false;
		
		dbConn.disconnect();
		if (dbConnMgr.freeDBConnection(dbConn)) {
			ret = true;
		}
		
		System.out.println("Staff " + username + " logged out.");

		
		return ret;
	}
	
	
	/*
	 * Creates new staff
	 */
	public boolean createStaff() throws SQLException {
		boolean ret = false;
		
		if (dbConn != null) {
			dbConnMgr.freeDBConnection(dbConn);
		}
		
		dbConn = dbConnMgr.getDBConnection();
		dbConn.connect();
		
		Integer nextAccountId = 1;
		ResultSet rs = dbConn.query("SELECT MAX(StaffId)+1 FROM STAFF");
		if (rs.next()) {
			nextAccountId = rs.getInt(1);
		}
		
		String sqlStmt = "INSERT INTO STAFF (StaffId, StaffName, Username, Password, " +
				"Email, DateCreated, DateModified) VALUES (" + nextAccountId + ", '" + name + "', '" +
				username + "', password('" + passwd + "'), '" + email + "', curdate(), curdate())";
		System.out.println(sqlStmt);
		dbConn.execSQL(sqlStmt);
		dbConn.execSQL("commit");

		dbConn.disconnect();
		
		ret = true;
		
		return ret;
	}
	
	
	/*
	 * Search open orders
	 */
	static public List<Order> searchOpenOrders() throws SQLException {
		List<Order> listOrder = new ArrayList<Order>();
		
		System.out.println("searchOpenOrders...");
		
		listOrder.addAll(Order.searchOrders(OrderStatus.InProgress));
		listOrder.addAll(Order.searchOrders(OrderStatus.Shipped));
		listOrder.addAll(Order.searchOrders(OrderStatus.Delivered));
		listOrder.addAll(Order.searchOrders(OrderStatus.Cancelled));
		
		return listOrder;
	}
	
	
	/*
	 * Retrieves Inventory instance
	 */
	public Inventory getInventory() {
		return Inventory.getInstance();
	}
	
	/*
	 * Returns the DBConnection
	 */
	public DBConnection getDBConnection() {
		return dbConn;
	}
	
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}


	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}


	/**
	 * @return the passwd
	 */
	public String getPasswd() {
		return passwd;
	}


	/**
	 * @param passwd the passwd to set
	 */
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}


	/**
	 * @return the staffId
	 */
	public int getStaffId() {
		return staffId;
	}


	/**
	 * @param staffId the staffId to set
	 */
	public void setStaffId(int staffId) {
		this.staffId = staffId;
	}


	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}


	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}




	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}


	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}


	/**
	 * @return the dateCreated
	 */
	public Date getDateCreated() {
		return dateCreated;
	}


	/**
	 * @param dateCreated the dateCreated to set
	 */
	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}


	/**
	 * @return the dateModified
	 */
	public Date getDateModified() {
		return dateModified;
	}


	/**
	 * @param dateModified the dateModified to set
	 */
	public void setDateModified(Date dateModified) {
		this.dateModified = dateModified;
	}



	
}
